<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>广告管理</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<style>
		.layui-input{width:130px;}
		.layui-form-label{width:auto;}
		.layui-form-item .layui-input-inline{width: 130px;}
		</style>
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">广告名称</label>
							<div class="layui-input-inline">
								<input type="text" name="title" placeholder="" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">展示时间</label>
							<div class="layui-input-inline" style="width:120px;">
								<input type="text" id="startTime" name="startTime" placeholder="开始时间" autocomplete="off" class="layui-input" style="width:120px;">
							</div>
							<div class="layui-input-inline" style="width:120px;">	
								<input type="text" id="endTime" name="endTime" placeholder="结束时间" autocomplete="off" class="layui-input" style="width:120px;">
							</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">广告终端</label>
							<div class="layui-input-inline">
							<select name="sltSpace" lay-verify="">
								<option value="">全部</option>
								{volist name="space" id="vo"}
								<option value="{$vo['ad_space_id']}">{$vo['ad_space_name']}</option>
								{/volist}
							</select>
							</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<label class="layui-form-label">启用状态</label>
							<div class="layui-input-inline">
							<select name="approve" lay-verify="">
								<option value="">全部</option>
								<option value="1">启用</option>
								<option value="-1">未启用</option>
							</select>
							</div>
						</div>
						
						<div class="layui-form-item layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="ad-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md">
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="ad-table" lay-filter="ad-table"></table>
			</div>
		</div>

		<script type="text/html" id="ad-toolbar">
			<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
		        <i class="layui-icon layui-icon-add-1"></i>
		        新增
		    </button>
		    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
		        <i class="layui-icon layui-icon-delete"></i>
		        删除
		    </button>
		</script>

		<script type="text/html" id="ad-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i></button>
		</script>

		<script type="text/html" id="ad-enable">
			<input type="checkbox" name="isEnabled" value="{{d.adId}}" lay-skin="switch" lay-text="启用|未启用" lay-filter="ad-enable" {{ d.isEnabled ==1 ? 'checked' : '' }}>
		</script>

		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery','common','laydate'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;
				let laydate = layui.laydate;
				
				laydate.render({
				    elem: '#endTime'
		  		});
		  	
		  		laydate.render({
			    elem: '#startTime'
	  			});
		  	
		  		let MODULE_PATH = "/{$MODULE_NAME}/portal/ad/";

				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '编号',
							field: 'adId',
							align: 'center',
							width: 100
						},
						{
							title: '广告名称',
							field: 'adName',
							align: 'center'
						},
						{
							title: '广告位',
							field: 'adSpaceName',
							align: 'center'
						},
						{
							title: '广告类型',
							field: 'category',
							align: 'center'
						},
						{
							title: '开始时间',
							field: 'startTime',
							align: 'center'
						},
						{
							title: '结束时间',
							field: 'endTime',
							align: 'center',
							templet: '#ad-createTime'
						},
						{
							title: '是否启用',
							field: 'isEnabled',
							align: 'center',
							templet: '#ad-enable',
						},
						{
							title: '操作',
							toolbar: '#ad-bar',
							align: 'center',
							width: 130
						}
					]
				]

				table.render({
					elem: '#ad-table',
					url: MODULE_PATH+"getAdList",
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#ad-toolbar',
					defaultToolbar: []
				});

				table.on('tool(ad-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(ad-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(ad-query)', function(data) {
					table.reload('ad-table', {
						where: data.field,
						page:{curr:1}
					})
					return false;
				});

				form.on('switch(ad-enable)', function(obj) {
					var is_enabled=0;
				   	if(obj.elem.checked){
				   		is_enabled=1;
				   	}
				    var id=this.value;
				    
				    $.ajax({
							url:MODULE_PATH+"updateEnable",
							data:{"id":id,'is_enabled':is_enabled},
							dataType: 'json',
							type: 'POST',
							success: function(result) {
								//如果不为空，则填充数据
								if(result.code){
									layer.msg("更新成功");
								}else{
									layer.msg("更新失败，请重试");
								}
							}
						})
				});

				window.add = function() {
					layer.open({
						type: 2,
						title: '新增广告',
						shade: 0.1,
						area: [common.isModile()?'100%':'800px', common.isModile()?'100%':'600px'],
						content: MODULE_PATH + 'view.html'
					});
				}

				window.edit = function(obj) {
					var _id=obj.data.adId;
					layer.open({
						type: 2,
						title: '编辑广告',
						shade: 0.1,
						area: ['800px', '600px'],
						content: MODULE_PATH + 'view/id/'+_id+'.html'
					});
				}

				window.remove = function(obj) {
					layer.confirm('确定要删除该广告?', {
						icon: 3,
						title: '删除提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "delete/id/" + obj.data['adId'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.batchRemove = function(obj) {
					
					var checkIds = common.checkField(obj,'adId');
					
					if (checkIds === "") {
						layer.msg("未选中数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					
					layer.confirm('确定要删除这些广告?', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "batchDelete/id/" + checkIds,
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										table.reload('ad-table');
									});
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				window.refresh = function(param) {
					table.reload('ad-table');
				}
			})
		</script>
	</body>
</html>
